//
//  UserInfoViewController.m
//  Lucky28
//
//  Created by zr on 2018/8/1.
//  Copyright © 2018年 zr. All rights reserved.
//

#import "UserInfoViewController.h"
#import "UserInfoHeaderView.h"
#import "UserInfoTableViewCell.h"
#import "UpdateNameViewController.h"
#import "PGDatePickManager.h"

@interface UserInfoViewController ()<UITableViewDelegate,UITableViewDataSource,PGDatePickerDelegate,UIImagePickerControllerDelegate, UINavigationControllerDelegate>{
    
    UIImagePickerController *_imagePickerController;
}

@property (nonatomic, weak)UITableView * tableView;
@property (nonatomic, strong)NSArray * showStringArray;
@property (nonatomic, strong)UIAlertController * alertVC;
@property (nonatomic, strong)UserInfoHeaderView *header;

@end

@implementation UserInfoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.showStringArray = @[@"昵称",@"性别",@"生日"];
    
    _imagePickerController = [[UIImagePickerController alloc] init];
    _imagePickerController.delegate = self;
    _imagePickerController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
    _imagePickerController.allowsEditing = YES;
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getPicture) name:@"GetPicture" object:nil];
    
    [self setupNav];
    [self setupUI];
    [self setupAlertVC];
}

- (void)setupNav{
    
    self.title = @"用户信息";
    
    UIButton * backButton = [[UIButton alloc] init];
    
    backButton.frame = CGRectMake(0, 0, 12 * SCREENH6, 20 * SCREENH6);
    [backButton setImage:[UIImage imageNamed:@"1返回"] forState:UIControlStateNormal];
    [backButton addTarget:self action:@selector(backToView) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem * leftButton = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    
    self.navigationItem.leftBarButtonItem = leftButton;
    
}


- (void)backToView{
    
    [self.navigationController popViewControllerAnimated:YES];
}


- (void)setupUI{
    
    UITableView * tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREENH_HEIGHT) style:UITableViewStylePlain];
    tableView.delegate = self;
    tableView.dataSource = self;
    tableView.backgroundColor = [UIColor whiteColor];
    tableView.rowHeight = 50 * SCREENH6;
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    tableView.scrollEnabled = NO;
    
    self.tableView = tableView;
    
    [self.tableView registerClass:[UserInfoTableViewCell class] forCellReuseIdentifier:@"userInfoListCell"];
    [self.tableView registerClass:[UserInfoHeaderView class] forHeaderFooterViewReuseIdentifier:@"userInfoHeaderView"];
    
    [self.view addSubview:self.tableView];
    
}

- (void)getPicture{
    
   [self presentViewController:self.alertVC animated:YES completion:nil];
}


- (void)setupAlertVC{
    
    self.alertVC = [UIAlertController alertControllerWithTitle:@"" message:@"选择照片方式" preferredStyle:UIAlertControllerStyleActionSheet];
    
    UIAlertAction * camerAction = [UIAlertAction actionWithTitle:@"相机" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        self->_imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
        
        [self presentViewController:self->_imagePickerController animated:YES completion:nil];
    }];
    
    UIAlertAction * albumsAction = [UIAlertAction actionWithTitle:@"相册" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        self->_imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        
        [self presentViewController:self->_imagePickerController animated:YES completion:nil];
    }];
    
    UIAlertAction * cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    
    [self.alertVC addAction:camerAction];
    [self.alertVC addAction:albumsAction];
    [self.alertVC addAction:cancelAction];
    
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(nullable NSDictionary<NSString *,id> *)editingInfo

{
    
//    [self.pictureButton setImage:image forState:UIControlStateNormal];
    self.header.iconImageView.image = image;
    
    NSData *frontImageData = UIImageJPEGRepresentation(image,0.1);
   
    [[ZSNetworkTool sharedTool] commitChangeMyIconWithfiles:frontImageData resultWithBlock:^(BOOL success, id responseData) {

        
//        NSLog(@"responseData==%@",responseData);
    }];
  
    [self dismissViewControllerAnimated:YES completion:nil];
    
}

//进入拍摄页面点击取消按钮

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker

{
    
    [self dismissViewControllerAnimated:YES completion:nil];
    
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    
    return 3;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UserInfoTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"userInfoListCell" forIndexPath:indexPath];
    
    cell.nameLabel.text = self.showStringArray[indexPath.row];
    
    if (indexPath.row == 0) {
        
        cell.string = self.name;
    }else if (indexPath.row == 1){
        
        cell.string = self.gender;
    }else if (indexPath.row == 2){
        
        cell.string = self.birthday;
    }
    
    return cell;
}


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 101 * SCREENH6;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    self.header = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"userInfoHeaderView"];
    
    
    [self.header.iconImageView sd_setImageWithURL:self.url placeholderImage:ImageNamed(@"头像7")];
    
    return self.header;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    HIDDEN_TAB;
    
    if (indexPath.row == 0) {
        
        UpdateNameViewController * updateNameVC = [UpdateNameViewController new];
        
        updateNameVC.returnValueBlock = ^(NSString *strValue) {
          
            self.header.nameString = strValue;
        };
        
        [self.navigationController pushViewController:updateNameVC animated:YES];
    }else if (indexPath.row == 1){
        
        [self creatAlertController_sheet];
    }else if (indexPath.row == 2){
        
        [self presentDateController];
    }
}



-(void)creatAlertController_sheet {

    UIAlertController *actionSheet = [UIAlertController alertControllerWithTitle:@"请选择性别" message:@"" preferredStyle:UIAlertControllerStyleActionSheet];

    UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"男" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        self.gender = @"男";
        
        [self.tableView reloadData];
        
        [[ZSNetworkTool sharedTool] submitChangePersonInfoWithUsername:nil andBirthday:nil sex:@"男" resultWithBlock:^(BOOL success, NSDictionary * responseData) {
            
//            NSLog(@"responseData===%@",responseData);
          
        }];
    }];
    UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"女" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        self.gender = @"女";
        
        [self.tableView reloadData];
        
        [[ZSNetworkTool sharedTool] submitChangePersonInfoWithUsername:nil andBirthday:nil sex:@"女" resultWithBlock:^(BOOL success, NSDictionary * responseData) {
            
//            NSLog(@"responseData===%@",responseData);
            
        }];
    }];
    
    UIAlertAction *action3 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
        
    }];
    
    //把action添加到actionSheet里
    [actionSheet addAction:action1];
    [actionSheet addAction:action2];
    [actionSheet addAction:action3];
    
    //相当于之前的[actionSheet show];
    [self presentViewController:actionSheet animated:YES completion:nil];
}


- (void)presentDateController{
    
    PGDatePickManager *datePickManager = [[PGDatePickManager alloc]init];
    datePickManager.isShadeBackgroud = true;
    PGDatePicker *datePicker = datePickManager.datePicker;
    datePicker.delegate = self;
    datePicker.datePickerType = PGDatePickerType1;
    datePicker.isHiddenMiddleText = false;
    datePicker.datePickerMode = PGDatePickerModeDate;
    [self presentViewController:datePickManager animated:false completion:nil];
}


#pragma PGDatePickerDelegate
- (void)datePicker:(PGDatePicker *)datePicker didSelectDate:(NSDateComponents *)dateComponents {
    
    self.birthday = [NSString stringWithFormat:@"%li-%02li-%02li",(long)dateComponents.year,dateComponents.month,dateComponents.day];
    
    [self.tableView reloadData];
    
    [[ZSNetworkTool sharedTool] submitChangePersonInfoWithUsername:nil andBirthday:self.birthday sex:nil resultWithBlock:^(BOOL success, NSDictionary * responseData) {

        NSLog(@"responseData===%@",responseData);

    }];
}


- (void)dealloc{
    
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
